import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ApiService } from 'src/app/core/api';
import { NzMessageService } from 'ng-zorro-antd';
import { MessageService } from 'src/app/core/service/message.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  loginForm2: FormGroup;
  subType = 'pwSub';
  sendCodeType = false;
  sendCodeNum = 10;
  _timer;
  ngOnInit() {
    this.messageService.setBreadcrumb({
      breadcrumbs: [
          { name: 'login' }
          ]
    });
    this.loginForm = this.fb.group({
      mobile: [null,
        [Validators.required]
      ],
      loginPw: [null, [Validators.required, Validators.pattern('(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,24}')]],
    });
    this.loginForm2 = this.fb.group({
      mobile: [null,
        [Validators.required]
      ],
      verifyCode: [null, [Validators.required]],
    });
  }
  submit() {
    if (this.loginForm.valid) {
      this.api.post('登录', this.loginForm.value).then((res: any) => {
        localStorage.setItem('token', res.data.token);
        this.getUser({ mobile: this.loginForm.value.mobile });
      }).catch((res) => {
        this.message.error(res.data.message);
      });
    }
  }

  submit2() {
    if (this.loginForm2.valid) {
      this.api.post('验证码登录', this.loginForm2.value).then((res: any) => {
        localStorage.setItem('token', res.data.token);
        localStorage.setItem('user', JSON.stringify(res.data));
        this.router.navigate(['pages/smrz']);
      }).catch((res) => {
        this.message.error(res.data.message);
      });
    }
  }

  toSendCode() {
    if (this.loginForm2.value['mobile']) {
      this.api.post('发送手机验证码', { mobile: this.loginForm2.value['mobile'], type: 'login' }).then(res => {
        this.sendCodeNum = res['data']['durationTime'];
        // this.api.msgSuccess(this.msgData['register-code']);
        this.sendCodeType = true;
        this._timer = setInterval(() => {
          if (this.sendCodeNum == 0) {
            this.sendCodeType = false;
            clearInterval(this._timer);
          } else {
            this.sendCodeNum--;
          }
        }, 1000);
      });
    } else {
      // this.api.msgEeeor(this.msgData['register-mail-error']);
    }
  }

  getUser(data) {
    this.api.post('用户详情', data).then(res => {
      let userMap = res['data']['enterpriseUserDetail'];
      if (userMap['idcardAuthStatus'] == 'APPROVED') {
        userMap = Object.assign(userMap, { isSMRZ: true });
      } else {
        userMap = Object.assign(userMap, { isSMRZ: false });
      };
      localStorage.setItem('user', JSON.stringify(userMap));
      this.messageService.sendMessage('layout', 'userOk')
      this.router.navigate(['/member/center/home/index']);
    }).catch(res => {
      this.message.error(res.data.message);
    });
  }

  changeSubType(type: string) {
    console.log(type)
    this.subType = type;
  }
  constructor(private fb: FormBuilder, private router: Router, private api: ApiService, private message: NzMessageService, private messageService: MessageService) { }
}
